/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-06-19 10:31:14
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-09-03 16:43:16
 * @FilePath: /etws-sxk2307/src/packetResolver/ddsCmdV1.cpp
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#include "ddsCmdV1.h"
#include "Server.h"

using namespace std;

std::map<int32_t, CallbackCmdFun> ddrCallbackMap = {
{0x00020000,ddr_callback_0},
{0x00020001,ddr_callback_1},
{0x00020002,ddr_callback_2},
{0x00020003,ddr_callback_3},
{0x00020004,ddr_callback_4},
{0x00020005,ddr_callback_5},
{0x00020006,ddr_callback_6},
{0x00020007,ddr_callback_7},
{0x00020008,ddr_callback_8},
{0x00020009,ddr_callback_9},
{0x00020000a,ddr_callback_10},
{0x00020000b,ddr_callback_11},
{0x00020000c,ddr_callback_12},
{0x00020000d,ddr_callback_13},
};


void ddrProtocolAnalysis(struct cmdDataType* cmd,uint8_t * cmdData)
{
    // 解析指令
    int32_t cmdId = (((uint32_t)cmd->cmdType << 16) | cmd->cmdChildType);
    if ((cmd->dst_DevType == CurDeviceType) || (cmd->dst_DevType == allDevice)) {
        if (((cmd->dst_DevNum == ComponentNO) || (cmd->dst_DevNum == allDevice))) {
            ;
        } else {
            qlog_e("命令中的主指令:%d,子指令:%d",cmd->cmdType,cmd->cmdChildType);
            qlog_e("设备类型:%d,设备编号:%d,与本设备不符",cmd->dst_DevType,cmd->dst_DevNum);
            return;
        }
    } else {
        return;
    }
    qlog_i("Recvice ddr cmd");
    qlog_hexdump("Ddr cmd",16,(uint8_t *)cmd,sizeof(struct cmdDataType));
    // 解析数据
    auto it = ddrCallbackMap.find(cmdId);
    if (it != ddrCallbackMap.end()) {
        it->second(cmd, cmdData);
    } else {
        qlog_e("这个ddr指令未实现..");
    }
}
/**
 * @brief: 用于标记当前应答的组件编号,基于雷象指令的定时开启
 * 当前函数主要用于雷象直接对下级组件控制，在规定时间内接收所有应答的指令然后去解析
 * @return {*}
 */
void ddsProtocolAnalysis(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if ((cmd->dst_DevType == CurDeviceType) || (cmd->dst_DevType == allDevice)){
        if(((cmd->dst_DevNum == ComponentNO) || (cmd->dst_DevNum == allDevice))) {
            ;
        }else{
            qlog_e("命令中的主指令:%d,子指令:%d",cmd->cmdType,cmd->cmdChildType);
            qlog_e("设备类型:%d,设备编号:%d,与本设备不符",cmd->dst_DevType,cmd->dst_DevNum);
            return;
        }
    } else {
        return;
    }
    qlog_i("Recvice dds cmd");
	qlog_hexdump("Dds cmd",16,(uint8_t *)cmd,sizeof(struct cmdDataType));
    if (Server::Instance().getSharePtr() != nullptr) { //接收到应答
        if (cmd->dst_DevType == CurDeviceType &&
            cmd->dst_DevNum == ComponentNO) {
                uint16_t id = cmd->source_DevType << 8 | cmd->source_DevNum;
                Server::Instance().getSharePtr()->addResponse(m_devMap[id], "successfully");
            }
    }
}


